package com.api.workflow.util;

import com.alibaba.fastjson.JSONArray;
import net.sf.json.JSONObject;
import weaver.general.BaseBean;
import weaver.workflow.webservices.*;

/**
 * @ClassName CreateWorkflowDetailUtil
 * @Description
 * @ahtuor wtx
 * @DATE 2024/9/16 9:07
 */

public class CreateWorkflowDetailUtil {
    /**
     * 创建流程带明细
     *
     * @param strRequestName 流程标题
     * @param strWorkflowId  流程id
     * @param userid         创建人
     * @param mainFields     主表信息
     * @param workflowDetail 明细表信息
     * @return
     */

    public static String createWorkflowDetail(String strRequestName, String strWorkflowId, String userid, String mainFields, String workflowDetail) {
        BaseBean baseBean = new BaseBean();
        //工作流程请求信息

        WorkflowRequestInfo workflowRequestInfo = new WorkflowRequestInfo();
        workflowRequestInfo.setCanView(true);//显示
        workflowRequestInfo.setCanEdit(true);//可编辑
        workflowRequestInfo.setRequestName(strRequestName);//请求标题
        workflowRequestInfo.setRequestLevel("0");//请求重要级别
        workflowRequestInfo.setCreatorId(userid);//创建者id
        workflowRequestInfo.setIsnextflow("0");//默认不提交流程
        WorkflowBaseInfo workflowBaseInfo = new WorkflowBaseInfo();//工作流信息
        workflowBaseInfo.setWorkflowId(strWorkflowId);//流程ID
        workflowRequestInfo.setWorkflowBaseInfo(workflowBaseInfo);//工作流信息


        //流程主表
        baseBean.writeLog("==============流程主表 start================");
        JSONObject headObj = JSONObject.fromObject(mainFields);
        Object[] headObjKeys = headObj.keySet().toArray();
        WorkflowMainTableInfo workflowMainTableInfo = new WorkflowMainTableInfo();
        WorkflowRequestTableRecord[] workflowRequestTableRecords = new WorkflowRequestTableRecord[1];
        workflowRequestTableRecords[0] = new WorkflowRequestTableRecord();
        //根据字段数判断需要创建的字段数组长度
        WorkflowRequestTableField[] workflowRequestTableFields = new WorkflowRequestTableField[headObjKeys.length];
        for (int i = 0; i < headObjKeys.length; i++) {
            String key = ((String) headObjKeys[i]); //字段名称
            String value = headObj.getString(key); //字段
            WorkflowRequestTableField workflowRequestTableField = new WorkflowRequestTableField();
            workflowRequestTableField.setFieldName(key);
            workflowRequestTableField.setFieldValue(value);
            workflowRequestTableField.setView(true);
            workflowRequestTableField.setEdit(true);
            workflowRequestTableFields[i] = workflowRequestTableField;
        }
        //将主表信息字段放入workflowRequestTableFields
        workflowRequestTableRecords[0].setWorkflowRequestTableFields(workflowRequestTableFields);
        //将workflowRequestTableRecords放入主表对象
        workflowMainTableInfo.setRequestRecords(workflowRequestTableRecords);
        baseBean.writeLog("==============流程主表 end======================" + workflowMainTableInfo.toString());
        workflowRequestInfo.setWorkflowMainTableInfo(workflowMainTableInfo); //流程主表信息


        baseBean.writeLog("==============流程明细表 start================");
        JSONArray headObjDetails = com.alibaba.fastjson.JSONObject.parseArray(workflowDetail);
        WorkflowDetailTableInfo[] workflowDetailTableInfos = new WorkflowDetailTableInfo[1];
        int size = headObjDetails.size();
        WorkflowRequestTableRecord[] workflowDetailRequestTableRecords = new WorkflowRequestTableRecord[size];
        for (int i = 0; i < headObjDetails.size(); i++) {
            JSONObject headObjDetail = JSONObject.fromObject(headObjDetails.get(i));
            Object[] headObjKeysDetail = headObjDetail.keySet().toArray();
            workflowDetailRequestTableRecords[i] = new WorkflowRequestTableRecord();
            //根据字段数判断需要创建的字段数组长度
            int length = headObjKeysDetail.length;
            WorkflowRequestTableField[] workflowDetailRequestTableFields = new WorkflowRequestTableField[length];
            for (int ii = 0; ii < headObjKeysDetail.length; ii++) {
                WorkflowRequestTableField workflowDetailRequestTableField = new WorkflowRequestTableField();
                String key = ((String) headObjKeysDetail[ii]); //字段名称
                String value = headObjDetail.getString(key); //字段
                workflowDetailRequestTableField.setFieldName(key);
                workflowDetailRequestTableField.setFieldValue(value);
                workflowDetailRequestTableField.setView(true);
                workflowDetailRequestTableField.setEdit(true);
                workflowDetailRequestTableFields[ii] = workflowDetailRequestTableField;
            }
            workflowDetailRequestTableRecords[i].setWorkflowRequestTableFields(workflowDetailRequestTableFields);
        }
        workflowDetailTableInfos[0] = new WorkflowDetailTableInfo();
        workflowDetailTableInfos[0].setWorkflowRequestTableRecords(workflowDetailRequestTableRecords);

        for (WorkflowDetailTableInfo workflowDetailTableInfo : workflowDetailTableInfos) {
            baseBean.writeLog("==============流程明细表 end======================" + com.alibaba.fastjson.JSONObject.toJSONString(workflowDetailTableInfo));
        }

        workflowRequestInfo.setWorkflowDetailTableInfos(workflowDetailTableInfos); //流程m明细表信息
        WorkflowServiceImpl workflowServiceImpl = new WorkflowServiceImpl();
        baseBean.writeLog("==============workflowRequestInfo======================" + JSONObject.fromObject(workflowRequestInfo));
        String requestid = workflowServiceImpl.doCreateWorkflowRequest(workflowRequestInfo, Integer.parseInt(userid));
        baseBean.writeLog("==============流程主表requestid:" + requestid);
        return requestid;
    }
}
